//
//  DerbyBoutState.h
//  DerbyOSSampler
//
//  Created by glenn andreas on 2/25/14.
//  Copyright (c) 2014 glenn andreas. All rights reserved.
//

#import <Foundation/Foundation.h>

enum {
    DerbyBoutStatePreBout = 0,  // the bout hasn't started yet (we are before the start time)
    DerbyBoutStatePending = 1, // we have reach the bell time, but haven't started yet
    DerbyBoutStateIntermission = 2, // in an intermission
    
    DerbyBoutStateJamIsOn = 10, // jam has started
    DerbyBoutStateLineup = 11, // waiting for lineup
        
    DerbyBoutStateOfficialTimeout = 20,
    DerbyBoutStateTeamTimeout = 21,
    DerbyBoutStateOfficialReviewTimeout = 22,
    DerbyBoutStateInjury = 23,
    DerbyBoutStatePostTimeoutLineup = 30, // the "up to 30 seconds" after a timeout
    
    DerbyBoutStateOvertime = 50,
    DerbyBoutStateFinal = 51, // unofficial final - switch to "completed" when done
    
    DerbyBoutStateCompleted = 100 // bout is completed, score is final
};

static inline BOOL DerbyBoutStateShouldPeriodClockRun(NSInteger state) {
    return state == DerbyBoutStateJamIsOn || state == DerbyBoutStateLineup;
}
static inline BOOL DerbyBoutStateShouldTimeoutClockRun(NSInteger state) {
    return DerbyBoutStateOfficialTimeout<= state && state < DerbyBoutStateInjury;
}
static inline BOOL DerbyBoutStateShouldJamClockRun(NSInteger state) {
    return state == DerbyBoutStateJamIsOn;
}
static inline BOOL DerbyBoutStateShouldIntermissionClockRun(NSInteger state) {
    return state == DerbyBoutStateIntermission;
}
static inline BOOL DerbyBoutStateShouldTimeToDerbuClockRun(NSInteger state) {
    return state == DerbyBoutStatePreBout;
}
static inline BOOL DerbyBoutStateShouldUnofficialFinalShow(NSInteger state) {
    return state == DerbyBoutStateFinal;
}
static inline BOOL DerbyBoutStateShouldOfficialFinalShow(NSInteger state) {
    return state == DerbyBoutStateCompleted;
}
static inline BOOL DerbyBoutStateShouldOvertimeShow(NSInteger state) {
    return state == DerbyBoutStateOvertime;
}
static inline BOOL DerbyBoutStateShouldLineupClockRun(NSInteger state) {
    return state == DerbyBoutStateLineup || state == DerbyBoutStatePostTimeoutLineup;
}


@class DXMLBoutObject;
@class DXMLJamObject;
@class DXMLPeriodObject;
@class DXMLObject;
@class DerbyStopwatch;
@class DXMLTeamObject;

@interface DerbyBoutTeamState : NSObject<NSCopying>
- (id) initWithTeam: (DXMLTeamObject *) team;
@property (readonly) DXMLTeamObject *team;
@property (assign) NSInteger timeoutsUsed;
@property (assign) NSInteger officalReviewsUsed; // this period
@property (readonly) NSInteger timeoutsLeft;
@property (readonly) NSInteger officalReviewsLeft; // this period
@property (assign) NSInteger score;
@property (assign) BOOL hasLead;
@property (strong) NSDictionary *lineup;
@end

@interface DerbyBoutState : NSObject<NSCopying>
- (id) initWithBout: (DXMLBoutObject *) derbyXML;
@property (assign) NSInteger boutState;
@property (readonly, nonatomic) BOOL shouldPeriodClockRun;
@property (readonly, nonatomic) BOOL shouldTimeoutClockRun;
@property (readonly, nonatomic) BOOL shouldJamClockRun;
@property (readonly, nonatomic) BOOL shouldIntermissionClockRun;
@property (readonly, nonatomic) BOOL shouldTimeToDerbuClockRun;
@property (readonly, nonatomic) BOOL shouldUnofficialFinalShow;
@property (readonly, nonatomic) BOOL shouldOfficialFinalShow;
@property (readonly, nonatomic) BOOL shouldOvertimeShow;
@property (readonly, nonatomic) BOOL shouldLineupClockRun;
/*
 */
@property (readonly) DXMLBoutObject *derbyXML;
@property (strong) DerbyBoutTeamState *homeTeamState;
@property (strong) DerbyBoutTeamState *awayTeamState;
@property (assign) NSInteger periodNumber;
@property (assign) NSInteger jamNumber;
// "Current" is the one about to happen, "active" is the one that is actually running (or has just completed)
@property (strong) DXMLJamObject *currentJam;
@property (strong) DXMLPeriodObject *currentPeriod;
@property (strong) DXMLJamObject *activeJam;
@property (strong) DXMLPeriodObject *activePeriod;
@property (strong) DXMLObject *activeTimeout;

// this can also auto-manage timers
@property (strong) DerbyStopwatch *periodTimer;
@property (strong) DerbyStopwatch *jamTimer;
@property (strong) DerbyStopwatch *lineupTimer;
@property (strong) DerbyStopwatch *timeoutTimer;
@property (strong) DerbyStopwatch *intermissionTimer;

- (void) updateBoutState: (DXMLObject *) xml;
@end
